package com.njwb.day15.zqzb.game;

public class Game {
    public static final String MESSAGE="1";
    //依赖玩家类
    private Player player ;

    //策略需要用户选择
    private IGenStrategy iGenStrategy ;
    public Player getPlayer() {
        return player;
    }

    public void setPlayer(Player player) {
        this.player = player;
    }

    public IGenStrategy getiGenStrategy() {
        return iGenStrategy;
    }

    public void setiGenStrategy(IGenStrategy iGenStrategy) {
        this.iGenStrategy = iGenStrategy;
    }

    public Game(Player player) {
        this.player = player;
    }
    /**
     * 返回变换的字符串
     * @return
     */
    public String genString(){
        return iGenStrategy.genString(this.player);
    }

    /**
     * 输入错误，游戏结束
     * 输入正确，看是否超时，如果超时，游戏结束
     *                   如果未超时，显示当前玩家的信息
     *                   判断是否通关
     * @param out :系统输出字符串
     * @param in  :用户的输入字符串
     */
    public void printResult(String out,String in){
        String txt=null;
Util util=new Util();
        if(!out.equals(in)) {
            System.out.println("输入错误，系统退出");
            System.exit(-1);
        }
      long endTime = System.currentTimeMillis() ;
        long diffTime = (endTime - this.getPlayer().getStartTime())/1000 ;
        this.getPlayer().setElapsedTime(diffTime);
        //分数累加
        int currentScore = this.getPlayer().getScore();
        //取出输入正确一次的得分
        int perScore = LevelParams.LEVELS[this.player.getLevelNo()-1].getPerScore();
        this.getPlayer().setScore(currentScore+perScore);





        if(this.getPlayer().getElapsedTime()<=LevelParams.LEVELS[this.player.getLevelNo()-1].getLimitedTime()) {
          String time=util.time(this.player.getStartTime());
      txt = "\n玩家:" + this.player.getName() +
                    " ,开始时间:" + time +
                    ",结束时间:" + this.player.getElapsedTime() +
                    ",最终得分:" + this.player.getScore();

            System.out.println(this.player.toString());
            util.writeTxt(txt);
            //通关判断。
            //取出最大的级别
            Level maxLevel = LevelParams.LEVELS[LevelParams.LEVELS.length-1];

            //达到最高级别
            if(this.player.getLevelNo()==maxLevel.getLevelNo()){
                    if(this.player.getScore()==maxLevel.getPerScore()*maxLevel.getStrTimes()) {

                        System.out.println("恭喜你，通关了");
                    }
            }

        }else{
            System.out.println("输入超时，系统退出");
            System.exit(-1);
        }


    }
}
